/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: UserService
 * Author:   kadibeieun
 * Date:     2019/9/3 16:34
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.afd.consumer.service;

import com.afd.consumer.api.UserFeignClient;
import com.afd.consumer.dao.UserDao;
import com.afd.consumer.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author kadibeieun
 * @create 2019/9/3
 * @since 1.0.0
 */
@Service
public class UserService {

    // Eureka客户端，可以获取到服务实例信息
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private UserDao userDao;

    @Autowired
    private UserFeignClient userFeignClient;

    public List<User> querUserByIds(List<Long> ids){
        List<User> users = new ArrayList<>();
        //String baseUrl = "http://localhost:8888/user/";
        // 地址直接写服务名称即可
        String baseUrl = "http://user-service/user/";
        // 根据服务名称，获取服务实例
       // List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
        // 因为只有一个UserService,因此我们直接get(0)获取
       // ServiceInstance serviceInstance = instances.get(0);
        // 获取ip和端口信息

        //String baseUrl = "http://"+serviceInstance.getHost() + ":" + serviceInstance.getPort()+"/user/";
        ids.forEach(id ->{
            // 我们测试多次查询，
            //users.add(this.userDao.queryUserById(id));

            users.add(this.userFeignClient.queryUserById(id));
//            try {
//                Thread.sleep(500);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        });
        return users;
    }

}